package com.lq.ecoldchain.binding

import android.app.Activity
import android.arch.lifecycle.MutableLiveData
import android.content.*
import android.databinding.BindingAdapter
import android.graphics.drawable.Drawable
import android.support.annotation.ColorInt
import android.support.design.widget.TabLayout
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.lq.ecoldchain.R
import com.lq.ecoldchain.base.view.App
import com.lq.ecoldchain.callback.SafeClickListener
import com.lq.ecoldchain.config.AppConfig
import com.lq.ecoldchain.util.StateUtil
import com.lq.ecoldchain.util.toast
import kotlinx.android.synthetic.main.activity_base.*
import java.util.*
import kotlin.collections.ArrayList

/*
*2018/12/22 0022  16:54
*function by lq
*/


@BindingAdapter("bind_state_view")
fun setStateViewParam(view:View,content:String){
    val param = view.layoutParams
    param.height = StateUtil.getStatusBarHeight(view.context)
    view.layoutParams = param
    view.setOnClickListener {
        toast { content }
    }
}

@BindingAdapter("toolbar_new")
fun setToolbarNew(mToolbar:Toolbar,content:String){
    val activity = mToolbar.context as AppCompatActivity
    activity.setSupportActionBar(mToolbar)
    activity.supportActionBar?.setDisplayShowTitleEnabled(false)
    activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

@BindingAdapter("bind_visibility")
fun setVisible(view: View, visible: Boolean) {
    view.visibility = if (visible) View.VISIBLE else View.GONE
}

@BindingAdapter("bind_gone")
fun setGone(view:View,gone:Boolean){
    view.visibility = if(gone) View.GONE else View.VISIBLE
}

@BindingAdapter("toFinish")
fun toOtherFinish(view: View, name: String) {
    view.setOnClickListener(object : SafeClickListener() {
        override fun onNoDoubleClick(v: View) {
            val activity = view.context as Activity
            val intent = Intent()
            intent.setClassName(activity, name)
            try {
                activity.startActivity(intent)
                activity.finish()
            } catch (e: ActivityNotFoundException) {
            }
        }
    })
}

@BindingAdapter("toOther")
fun toOther(view: View, path: String) {
    view.setOnClickListener(object : SafeClickListener() {
        override fun onNoDoubleClick(v: View) {
            ARouter.getInstance().build(path).navigation()
        }
    })
}


@BindingAdapter("finish")
fun setFinish(view: View, finished: MutableLiveData<Boolean>) {
    if (finished.value == true) {
        val activity = view.context as Activity
        activity.finish()
    }
}


@BindingAdapter("edit_visible")
fun setEditVisible(view: EditText, canSee: MutableLiveData<Boolean>) {
    when (canSee.value) {
        true -> {
            view.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD// 输入为密码且可见
        }
        false -> {
            view.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT//不可见
        }
        else -> {
            view.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT//不可见
        }
    }
    view.setSelection(view.text.length)
}


@BindingAdapter("pwd_visible_iv")
fun changePwdAgainState(view: ImageView, see: MutableLiveData<Boolean>) {
    view.setOnClickListener {
        see.value = see.value != true
        when (see.value) {
            true -> {
                view.setImageResource(R.drawable.according_icon)
            }
            else -> {
                view.setImageResource(R.drawable.hidden_icon)
            }
        }
    }
}

@BindingAdapter("bind_page_adapter")
fun bindPagerAdapterA(view: ViewPager, adapter: FragmentStatePagerAdapter) {
    view.adapter = adapter
}

@BindingAdapter("bind_tab_viewPager", requireAll = true)
fun setTabViewPager(tab: TabLayout, view: ViewPager) = tab.setupWithViewPager(view)

@BindingAdapter("bind_tab_adapter")
fun setTabAdapter(view: TabLayout, list: List<View>) {
    if (!list.isEmpty())
        for (i in 0 until list.size) {
            view.getTabAt(i)?.customView = list[i]
        }
}

@BindingAdapter("bind_tab_title")
fun setTableTitle(view: TabLayout, list: ArrayList<String>) {
    if (!list.isEmpty())
        for (i in 0 until list.size) {
            view.addTab(view.newTab())
            view.getTabAt(i)?.text = list[i]
        }
}

@BindingAdapter("bind_edit_num")
fun setEditNum(view: TextView, edit: EditText) {
    edit.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            view.text = "${edit.text.length}/300"
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            view.text = "${edit.text.length}/300"
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            view.text = "${edit.text.length}/300"
        }
    })
}

/*复制文本到粘贴板*/
@BindingAdapter("bind_clip_text")
fun setClipText(view: View, content: MutableLiveData<String>) {
    view.setOnClickListener {
        if(!content.value.isNullOrEmpty()){
            val cm = App.instance.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val data = ClipData.newPlainText(AppConfig.INVITE_CODE, content.value!!)
            cm.primaryClip = data
            toast { "复制成功" }
        }
    }
}






